import { defineApis } from '@/utils/api'
// const grantType = 'password'
const grantType = 'password_captcha'

  
import qs from "qs";

export type LoginParams = {
  username?: string
  password?: string
  code?: string
  cacheKey?: string
}


// 登录方法
function login(loginParams: LoginParams = {}) {
  const {username, password, code, cacheKey} = loginParams
  return {
    url: "/oauth/token",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
    method: "POST",
    params: {
      grant_type: grantType,
      cacheKey,
      code,
    },
    data: qs.stringify({
      grant_type: grantType,
      username,
      password,
      scope: "server",
      cacheKey,
      code,
    }),
    auth: {
      username: "admin",
      password: "admin",
    },
  };
}

// 注册方法
function register(data: any) {
  return {
    url: "/register",
    headers: {
      isToken: false,
    },
    method: "post",
    data: data,
  };
}

// 获取当前用户的详细信息
const getCurrentUserInfo = {
  url: "/user/me",
  method: "get",
  params: {
    group: 'local'
  }
};


// 退出方法
const logout = {
  url: "/logout",
  method: "DELETE",
};


export default defineApis('admin', {
  login,
  register,
  getCurrentUserInfo,
  logout
})